/**********************************************************************
 * 	SuperSprite 类的扩展，增加四个基本效果控制方法及五个事件驱动
 * 	外部使用（或子类复写）接口：
 * 			Show()			以缩放方式进行显示
 * 			Hidden()		以缩放方式进行隐藏
 * 			Display()		以淡入方式进行显示
 * 			Clear()			以淡出方式进行隐藏
 * 	事件驱动：
 * 			SCALE_SHOW		采用缩放方式显示 结束时触发
 * 			SCALE_HIDDEN	采用缩放方式隐藏 结束时触发
 * 
 * 			FADE_SHOW		采用淡入方式显示 结束时触发
 * 			FADE_HIDDEN		采用淡出方式隐藏 结束时触发
 * 			
 * 			SHOW_DONE		任意一种显示方式 结束时触发
 * 			HIDDEN_DONE		任意一种隐藏方式 结束时触发
 ***********************************************************************/
package org.dstart.csuper
{
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class ExSuperSprite extends SuperSprite
	{
		public static const SCALE_SHOW:String	= "scaleShow";		//	平滑显示
		public static const SCALE_HIDDEN:String	= "scaleHidden";	//	平滑消失
		
		public static const SHOW_DONE:String	= "showDone";		//	显示完毕
		public static const HIDDEN_DONE:String	= "hiddenDone";		//	隐藏完毕
		
		public static const FADE_SHOW:String	= "fadeShow";		//	淡入显示
		public static const FADE_HIDDEN:String	= "fadeHidden";		//	淡出消失
		
		public static var isPlayMovie:Boolean = true;		//	是否允许平滑动画

		private var m_xBase:Number;		//	对象的初始 X 坐标
		private var m_yBase:Number;		//	对象的初始 Y 坐标
		private var m_width:Number;		//	对象的初始宽度值
		private var m_height:Number;	//	对象的初始高度值

		private var m_timer:Timer;		//	动作时间控制
		private var m_isShow:Boolean;	//	是否正在播放显示动画
		private var m_isHidden:Boolean;	//	是否正在播放隐藏动画

		public function ExSuperSprite()
		{
			super();
			
			InitValue();
		}

		/**	显示对象（设置初始值并开始播放显示动画） */
		public function Show():void
		{
			if(!m_isShow)
			{
				if(isPlayMovie)
				{					
					m_isShow = true;

					SaveValue();
							
					this.x = m_xBase + m_width/2;
					this.y = m_yBase + m_height/2;
					
					this.scaleX = 0;
					this.scaleY = 0;
					this.alpha = 0;

					this.visible = true;
					
					m_timer.addEventListener(TimerEvent.TIMER, onTimerOpen);
					m_timer.start();
				}
				else
				{
					this.visible = true;
					this.dispatchEvent(new Event(ExSuperSprite.SCALE_SHOW));
					this.dispatchEvent(new Event(ExSuperSprite.SHOW_DONE));
				}
			}
		}
		
		/**	隐藏对象（隐藏对象，但并不让对象消失） */
		public function Hidden():void
		{
			if(!m_isHidden)
			{
				if(isPlayMovie)
				{
					m_isHidden = true;

					SaveValue();

					this.visible = true;		
					
					m_timer.addEventListener(TimerEvent.TIMER, onTimerClose);
					m_timer.start();
				}
				else
				{
					this.visible = false;
					this.dispatchEvent(new Event(ExSuperSprite.SCALE_HIDDEN));
					this.dispatchEvent(new Event(ExSuperSprite.HIDDEN_DONE));
				}
			}
		}
		
		/**	淡出显示 */
		public function Display():void
		{
			if(!m_isShow)
			{
				if(isPlayMovie)
				{
					m_isShow = true;
				
					SaveValue();
					
					this.alpha = 0;
					this.visible = true;
					
					m_timer.addEventListener(TimerEvent.TIMER, onSpriteDisplay);
					m_timer.start();
				}
				else
				{
					this.visible = true;
					this.dispatchEvent(new Event(ExSuperSprite.FADE_SHOW));
					this.dispatchEvent(new Event(ExSuperSprite.SHOW_DONE));
				}
			}
		}
		
		/**	淡出消失 */
		public function Clear():void
		{
			if(!m_isHidden)
			{
				if(isPlayMovie)
				{
					m_isHidden = true;
				
					SaveValue();
	
					// trace("do");
					this.visible = true;
					this.alpha = 1;
	
					m_timer.addEventListener(TimerEvent.TIMER, onSpriteClear);
					m_timer.start();
				}
				else
				{
					this.visible = false;
					this.dispatchEvent(new Event(ExSuperSprite.FADE_HIDDEN));
					this.dispatchEvent(new Event(ExSuperSprite.HIDDEN_DONE));
				}
			}
		}
		
		/***************************************************************************/
		
		/** 初始化相关变量初值 */
		private function InitValue():void
		{			
			m_xBase = this.x;
			m_yBase = this.y;
			
			this.x = m_xBase;
			this.y = m_yBase;
			this.visible = true;
			this.m_isShow = false;
			this.m_isHidden = false;
			
			m_timer = new Timer(20);
			
			// trace("init:",this.width,this.height);
		}
		
		/**	保存当前的一些重要参数值		*/
		private function SaveValue():void
		{
			m_xBase = this.x;
			m_yBase = this.y;
			m_width = this.width;
			m_height = this.height;
						
			OpenBitmap();
		}

		/**	窗口出现的动画脚本	*/
		private function onTimerOpen(e:TimerEvent):void
		{
			if(this.scaleX < 1 || this.scaleY < 1)
			{
				this.scaleX += 0.1;
				this.scaleY += 0.1;
				this.x = m_xBase + (m_width - this.width)/2;
				this.y = m_yBase + (m_height - this.height)/2;
			}
			this.alpha += 0.1;

			if(this.scaleX >= 1 && this.scaleY >= 1 && this.alpha >= 1)
			{
				m_timer.removeEventListener(TimerEvent.TIMER, onTimerOpen);
				m_timer.stop();
				
				this.m_isShow = false;
				this.scaleX = 1;
				this.scaleY = 1;
				this.alpha = 1;
				this.dispatchEvent(new Event(ExSuperSprite.SCALE_SHOW));
				this.dispatchEvent(new Event(ExSuperSprite.SHOW_DONE));
				
				CloseBitmap();
			}
			
			e.updateAfterEvent();
		}
		
		/**	窗口消失的动画脚本	*/
		private function onTimerClose(e:TimerEvent):void
		{
			this.scaleX -= 0.1;
			this.scaleY -= 0.1;
			this.x = m_xBase + (m_width - this.width)/2;
			this.y = m_yBase + (m_height - this.height)/2;
			this.alpha -= 0.1;

			if(this.scaleX <= 0 || this.scaleY <= 0 || this.alpha <= 0)
			{
				m_timer.removeEventListener(TimerEvent.TIMER, onTimerClose);
				m_timer.stop();		
						
				this.m_isHidden = false;
				this.scaleX = 0;
				this.scaleY = 0;
				this.alpha = 0;
				this.visible = false;
				this.dispatchEvent(new Event(ExSuperSprite.SCALE_HIDDEN));
				this.dispatchEvent(new Event(ExSuperSprite.HIDDEN_DONE));
				
				CloseBitmap();
			}
			
			e.updateAfterEvent();
		}
		
		/** 窗口淡入动画脚本 */
		private function onSpriteDisplay(e:TimerEvent):void
		{
			this.alpha += 0.1;
			
			if(this.alpha >= 1)
			{				
				m_timer.removeEventListener(TimerEvent.TIMER, onSpriteDisplay);
				m_timer.stop();	
				
				this.m_isShow = false;				
				this.alpha = 1;						
				this.dispatchEvent(new Event(ExSuperSprite.FADE_SHOW));			
				this.dispatchEvent(new Event(ExSuperSprite.SHOW_DONE));
				
				CloseBitmap();
			}
		}
		
		/** 窗口淡出动画脚本 */
		private function onSpriteClear(e:TimerEvent):void
		{
			this.alpha -= 0.1;
			
			if(this.alpha <= 0)
			{
				m_timer.removeEventListener(TimerEvent.TIMER, onSpriteClear);
				m_timer.stop();	
				
				this.m_isHidden = false;			
				this.alpha = 0;
				this.visible = false;	
				this.dispatchEvent(new Event(ExSuperSprite.FADE_HIDDEN));		
				this.dispatchEvent(new Event(ExSuperSprite.HIDDEN_DONE));
				
				CloseBitmap();
			}			
		}
	}
}